package com.liutao.service.impl;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liutao.dao.TalentsDao;
import com.liutao.entity.Talents;
import com.liutao.entity.Title;
import com.liutao.service.TalentsService;
@Service
public class TalentsServiceImpl implements TalentsService{
	@Autowired
	private TalentsDao dao;
	
	//用于模糊查询职称
	@Override
	public List<Title> findTitleNames() {
		// TODO Auto-generated method stub
		return dao.findTitleNames();
	}
	
	//列表
	@Override
	public PageInfo<Talents> list(Integer pageNum, Talents talents) {
		PageHelper.startPage(pageNum, 5);
		List<Talents> list = dao.list(talents);
		PageInfo<Talents> info = new PageInfo<Talents>(list);
		return info;
	}

	@Override
	public void add(Talents talents) {
		//添加主表
		dao.add(talents);
		//添加到中间表
		Integer[] tids = talents.getTids();
		if(tids != null) {
			for (Integer tid : tids) {
				dao.addCenter(talents.getId(),tid);
			}
		}
	}

	@Override
	public Talents findById(Integer id) {
		// TODO Auto-generated method stub
		return dao.findById(id);
	}

	@Override
	public Integer[] findCenterById(Integer id) {
		// TODO Auto-generated method stub
		return dao.findCenterById(id);
	}

	@Override
	public void update(@Valid Talents talents) {
		//1.删除中间表
		Integer id = talents.getId();
		dao.delCenter(id);
		//2.修改人才表
		dao.update(talents);
		//3.添加中间表
		Integer[] tids = talents.getTids();
		if(tids != null) {
			for (Integer tid : tids) {
				dao.addCenter(talents.getId(),tid);
			}
		}
		
		
	}

	@Override
	public void del(Integer id) {
		//删除中间表
		dao.delCenter(id);
		//删除主表
		dao.delTalents(id);
		
	}

	
}
